#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>


typedef struct s_list  *t_list;

struct s_list
{
  int                   val;
  t_list                next;
};

t_list empty(void)
{
	return NULL;
		
}
int is_empty(t_list l)
{
	return l == NULL;	
}
t_list add(int x, t_list l)
{
	t_list new = malloc (sizeof(t_list));
	new->val = x;
	new->next = l;
	return new;
}


size_t length (t_list l)
{
	return (l!=NULL?1+length(l->next):0);
}

int sum (t_list l)
{
	return (l!=NULL?l->val+sum(l->next):0);
}

float avg (t_list l)
{
	size_t		length=0;
	float 		avg=0;
	while ( l )
	{
		avg =+ l->val;
		l = l->next;
		length++;
	}
	return avg / length;
}

void destroy (t_list l)
{
	t_list		tmp;
	while ( l )
        {
		tmp = l->next;
		free (l);
		l = tmp;
        }
}

void iter(void (*f)(int), t_list l)
{
	while ( l )
        {
		f(l->val);
		l = l->next;
        }

}

void print_int(int x)
{
  printf(" %d;",x);
}
 
int main(int argc, char *argv[])
{
  t_list		l,tmp;
  size_t		len=5, i;
 
  if (argc>1)
    len = atoi(argv[1]);
 
  l = empty();
  for (i=1; i<=len; i++)
    l = add(i,l);
 
  /* avec iter */
  printf("iter : l = [");
  iter(print_int, l);
  printf("]\n");
 
  /* sans iter */
  printf("for : l = [");
  for (tmp = l; tmp; tmp = tmp->next)
    print_int(tmp->val);
  printf("]\n");
 
  printf("l: %u - %d - %.2f\n",length(l),sum(l), avg(l));
 
  destroy(l);
 
  return 0;
}

